{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 验证码识别\n",
    "将一个任务识别四个数字转化为四个任务分别一个数字，实现多任务识别。\n",
    "![多任务图](http://img.blog.csdn.net/20170315192855733?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWWFuX0pveQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)\n",
    "\n",
    "训练方法主要有两种，一种是交替训练，一种是联合训练。\n",
    "![交替训练](http://img.blog.csdn.net/20170315194721944?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWWFuX0pveQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)\n",
    "![联合训练](http://img.blog.csdn.net/20170315195612604?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWWFuX0pveQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tensorflow as tf \n",
    "from PIL import Image\n",
    "from nets import nets_factory\n",
    "import numpy as np\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 不同字符数量\n",
    "CHAR_SET_LEN = 10\n",
    "# 图片高度\n",
    "IMAGE_HEIGHT = 60 \n",
    "# 图片宽度\n",
    "IMAGE_WIDTH = 160  \n",
    "# 批次\n",
    "BATCH_SIZE = 25\n",
    "# tfrecord文件存放路径\n",
    "TFRECORD_FILE = \"captcha/train.tfrecords\"\n",
    "\n",
    "# placeholder\n",
    "x = tf.placeholder(tf.float32, [None, 224, 224])  \n",
    "y0 = tf.placeholder(tf.float32, [None]) \n",
    "y1 = tf.placeholder(tf.float32, [None]) \n",
    "y2 = tf.placeholder(tf.float32, [None]) \n",
    "y3 = tf.placeholder(tf.float32, [None])\n",
    "\n",
    "# 学习率\n",
    "lr = tf.Variable(0.003, dtype=tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 从tfrecord读出数据\n",
    "def read_and_decode(filename):\n",
    "    # 根据文件名生成一个队列\n",
    "    filename_queue = tf.train.string_input_producer([filename])\n",
    "    reader = tf.TFRecordReader()\n",
    "    # 返回文件名和文件\n",
    "    _, serialized_example = reader.read(filename_queue)   \n",
    "    features = tf.parse_single_example(serialized_example,\n",
    "                                       features={\n",
    "                                           'image' : tf.FixedLenFeature([], tf.string),\n",
    "                                           'label0': tf.FixedLenFeature([], tf.int64),\n",
    "                                           'label1': tf.FixedLenFeature([], tf.int64),\n",
    "                                           'label2': tf.FixedLenFeature([], tf.int64),\n",
    "                                           'label3': tf.FixedLenFeature([], tf.int64),\n",
    "                                       })\n",
    "    # 获取图片数据\n",
    "    image = tf.decode_raw(features['image'], tf.uint8)\n",
    "    # tf.train.shuffle_batch必须确定shape\n",
    "    image = tf.reshape(image, [224, 224])\n",
    "    # 图片预处理\n",
    "    image = tf.cast(image, tf.float32) / 255.0\n",
    "    image = tf.subtract(image, 0.5)\n",
    "    image = tf.multiply(image, 2.0)\n",
    "    # 获取label\n",
    "    label0 = tf.cast(features['label0'], tf.int32)\n",
    "    label1 = tf.cast(features['label1'], tf.int32)\n",
    "    label2 = tf.cast(features['label2'], tf.int32)\n",
    "    label3 = tf.cast(features['label3'], tf.int32)\n",
    "\n",
    "    return image, label0, label1, label2, label3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-4-782df4a11023>:30: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n",
      "开始进行训练(CPU):\n",
      "Iter:0  Loss:1863.396  Accuracy:0.28,0.16,0.20,0.20  Learning_rate:0.0010\n",
      "timeCost:0:00:17.470064\n",
      "Iter:20  Loss:2.308  Accuracy:0.08,0.16,0.08,0.20  Learning_rate:0.0010\n",
      "timeCost:0:02:26.692594\n",
      "Iter:40  Loss:2.298  Accuracy:0.12,0.12,0.16,0.12  Learning_rate:0.0010\n",
      "timeCost:0:04:33.662209\n",
      "Iter:60  Loss:2.290  Accuracy:0.12,0.12,0.08,0.08  Learning_rate:0.0010\n",
      "timeCost:0:06:38.423301\n",
      "Iter:80  Loss:2.323  Accuracy:0.16,0.08,0.08,0.12  Learning_rate:0.0010\n",
      "timeCost:0:08:42.787990\n",
      "Iter:100  Loss:2.297  Accuracy:0.12,0.16,0.16,0.16  Learning_rate:0.0010\n",
      "timeCost:0:10:47.068455\n",
      "Iter:120  Loss:2.314  Accuracy:0.08,0.16,0.12,0.04  Learning_rate:0.0010\n",
      "timeCost:0:12:53.715363\n",
      "Iter:140  Loss:2.291  Accuracy:0.08,0.16,0.08,0.08  Learning_rate:0.0010\n",
      "timeCost:0:15:00.302963\n",
      "Iter:160  Loss:2.301  Accuracy:0.16,0.12,0.12,0.16  Learning_rate:0.0010\n",
      "timeCost:0:17:13.828846\n",
      "Iter:180  Loss:2.296  Accuracy:0.04,0.08,0.20,0.08  Learning_rate:0.0010\n",
      "timeCost:0:19:21.292081\n",
      "Iter:200  Loss:2.297  Accuracy:0.16,0.08,0.16,0.12  Learning_rate:0.0010\n",
      "timeCost:0:21:26.015772\n",
      "Iter:220  Loss:2.291  Accuracy:0.12,0.12,0.20,0.12  Learning_rate:0.0010\n",
      "timeCost:0:23:31.306680\n",
      "Iter:240  Loss:2.284  Accuracy:0.20,0.04,0.08,0.20  Learning_rate:0.0010\n",
      "timeCost:0:25:38.365949\n",
      "Iter:260  Loss:2.321  Accuracy:0.16,0.12,0.04,0.00  Learning_rate:0.0010\n",
      "timeCost:0:27:56.575647\n",
      "Iter:280  Loss:2.289  Accuracy:0.12,0.28,0.04,0.08  Learning_rate:0.0010\n",
      "timeCost:0:30:06.146162\n",
      "Iter:300  Loss:2.322  Accuracy:0.00,0.16,0.16,0.08  Learning_rate:0.0010\n",
      "timeCost:0:32:11.093722\n",
      "Iter:320  Loss:2.288  Accuracy:0.12,0.04,0.16,0.04  Learning_rate:0.0010\n",
      "timeCost:0:34:15.797540\n",
      "Iter:340  Loss:2.312  Accuracy:0.20,0.08,0.16,0.04  Learning_rate:0.0010\n",
      "timeCost:0:36:20.349663\n",
      "Iter:360  Loss:2.301  Accuracy:0.16,0.08,0.08,0.12  Learning_rate:0.0010\n",
      "timeCost:0:38:25.052510\n",
      "Iter:380  Loss:2.305  Accuracy:0.04,0.08,0.12,0.08  Learning_rate:0.0010\n",
      "timeCost:0:40:29.506023\n",
      "Iter:400  Loss:2.295  Accuracy:0.16,0.12,0.20,0.16  Learning_rate:0.0010\n",
      "timeCost:0:42:34.309040\n",
      "Iter:420  Loss:2.309  Accuracy:0.16,0.12,0.08,0.08  Learning_rate:0.0010\n",
      "timeCost:0:44:39.043602\n",
      "Iter:440  Loss:2.299  Accuracy:0.16,0.08,0.12,0.08  Learning_rate:0.0010\n",
      "timeCost:0:46:43.067024\n",
      "Iter:460  Loss:2.293  Accuracy:0.08,0.12,0.12,0.12  Learning_rate:0.0010\n",
      "timeCost:0:48:47.375145\n",
      "Iter:480  Loss:2.298  Accuracy:0.12,0.00,0.16,0.16  Learning_rate:0.0010\n",
      "timeCost:0:50:52.129399\n",
      "Iter:500  Loss:2.308  Accuracy:0.04,0.00,0.16,0.04  Learning_rate:0.0010\n",
      "timeCost:0:52:57.058907\n",
      "Iter:520  Loss:2.300  Accuracy:0.20,0.08,0.12,0.20  Learning_rate:0.0010\n",
      "timeCost:0:56:08.212338\n",
      "Iter:540  Loss:2.311  Accuracy:0.08,0.08,0.04,0.20  Learning_rate:0.0010\n",
      "timeCost:0:59:37.710031\n",
      "Iter:560  Loss:2.302  Accuracy:0.12,0.04,0.04,0.28  Learning_rate:0.0010\n",
      "timeCost:1:03:07.242871\n",
      "Iter:580  Loss:2.300  Accuracy:0.08,0.08,0.28,0.12  Learning_rate:0.0010\n",
      "timeCost:1:06:36.869838\n",
      "Iter:600  Loss:2.295  Accuracy:0.12,0.20,0.16,0.12  Learning_rate:0.0010\n",
      "timeCost:1:09:59.730580\n",
      "Iter:620  Loss:2.297  Accuracy:0.08,0.24,0.04,0.08  Learning_rate:0.0010\n",
      "timeCost:1:13:22.997388\n",
      "Iter:640  Loss:2.329  Accuracy:0.00,0.04,0.12,0.12  Learning_rate:0.0010\n",
      "timeCost:1:16:46.081724\n",
      "Iter:660  Loss:2.284  Accuracy:0.04,0.12,0.16,0.12  Learning_rate:0.0010\n",
      "timeCost:1:20:08.629631\n",
      "Iter:680  Loss:2.297  Accuracy:0.24,0.08,0.12,0.04  Learning_rate:0.0010\n",
      "timeCost:1:23:31.288835\n",
      "Iter:700  Loss:2.259  Accuracy:0.20,0.12,0.12,0.16  Learning_rate:0.0010\n",
      "timeCost:1:26:54.156107\n",
      "Iter:720  Loss:2.273  Accuracy:0.20,0.20,0.20,0.16  Learning_rate:0.0010\n",
      "timeCost:1:30:16.776205\n",
      "Iter:740  Loss:2.106  Accuracy:0.16,0.24,0.28,0.16  Learning_rate:0.0010\n",
      "timeCost:1:33:39.317398\n",
      "Iter:760  Loss:2.143  Accuracy:0.20,0.08,0.40,0.20  Learning_rate:0.0010\n",
      "timeCost:1:37:02.450535\n",
      "Iter:780  Loss:2.173  Accuracy:0.24,0.16,0.08,0.08  Learning_rate:0.0010\n",
      "timeCost:1:40:25.780195\n",
      "Iter:800  Loss:2.164  Accuracy:0.32,0.16,0.32,0.08  Learning_rate:0.0010\n",
      "timeCost:1:43:48.648271\n",
      "Iter:820  Loss:2.097  Accuracy:0.24,0.36,0.16,0.12  Learning_rate:0.0010\n",
      "timeCost:1:47:11.410420\n",
      "Iter:840  Loss:2.106  Accuracy:0.32,0.12,0.24,0.16  Learning_rate:0.0010\n",
      "timeCost:1:50:34.060272\n",
      "Iter:860  Loss:2.071  Accuracy:0.40,0.12,0.16,0.12  Learning_rate:0.0010\n",
      "timeCost:1:53:57.000904\n",
      "Iter:880  Loss:1.944  Accuracy:0.32,0.16,0.28,0.16  Learning_rate:0.0010\n",
      "timeCost:1:57:19.766557\n",
      "Iter:900  Loss:1.982  Accuracy:0.28,0.20,0.40,0.32  Learning_rate:0.0010\n",
      "timeCost:2:00:42.435458\n",
      "Iter:920  Loss:1.905  Accuracy:0.40,0.16,0.20,0.24  Learning_rate:0.0010\n",
      "timeCost:2:04:05.346005\n",
      "Iter:940  Loss:1.911  Accuracy:0.28,0.12,0.12,0.52  Learning_rate:0.0010\n",
      "timeCost:2:07:28.539302\n",
      "Iter:960  Loss:1.834  Accuracy:0.40,0.16,0.16,0.20  Learning_rate:0.0010\n",
      "timeCost:2:10:51.486945\n",
      "Iter:980  Loss:1.840  Accuracy:0.36,0.20,0.28,0.44  Learning_rate:0.0010\n",
      "timeCost:2:14:13.913710\n",
      "Iter:1000  Loss:1.746  Accuracy:0.56,0.20,0.24,0.28  Learning_rate:0.0010\n",
      "timeCost:2:17:36.526043\n",
      "Iter:1020  Loss:1.731  Accuracy:0.48,0.24,0.24,0.24  Learning_rate:0.0010\n",
      "timeCost:2:20:58.449965\n",
      "Iter:1040  Loss:1.787  Accuracy:0.60,0.32,0.28,0.16  Learning_rate:0.0010\n",
      "timeCost:2:24:21.076755\n",
      "Iter:1060  Loss:1.576  Accuracy:0.52,0.32,0.40,0.48  Learning_rate:0.0010\n",
      "timeCost:2:27:44.521424\n",
      "Iter:1080  Loss:1.616  Accuracy:0.48,0.32,0.32,0.28  Learning_rate:0.0010\n",
      "timeCost:2:31:07.316661\n",
      "Iter:1100  Loss:1.376  Accuracy:0.68,0.40,0.28,0.36  Learning_rate:0.0010\n",
      "timeCost:2:34:30.246258\n",
      "Iter:1120  Loss:1.485  Accuracy:0.56,0.40,0.44,0.48  Learning_rate:0.0010\n",
      "timeCost:2:37:52.741700\n",
      "Iter:1140  Loss:1.412  Accuracy:0.76,0.24,0.36,0.40  Learning_rate:0.0010\n",
      "timeCost:2:41:15.678313\n",
      "Iter:1160  Loss:1.509  Accuracy:0.68,0.36,0.36,0.40  Learning_rate:0.0010\n",
      "timeCost:2:44:39.325386\n",
      "Iter:1180  Loss:1.197  Accuracy:0.84,0.40,0.40,0.52  Learning_rate:0.0010\n",
      "timeCost:2:48:02.620955\n",
      "Iter:1200  Loss:1.177  Accuracy:0.80,0.68,0.24,0.48  Learning_rate:0.0010\n",
      "timeCost:2:51:25.219672\n",
      "Iter:1220  Loss:1.079  Accuracy:0.88,0.40,0.48,0.52  Learning_rate:0.0010\n",
      "timeCost:2:54:48.486162\n",
      "Iter:1240  Loss:1.036  Accuracy:0.84,0.64,0.44,0.64  Learning_rate:0.0010\n",
      "timeCost:2:58:11.349089\n",
      "Iter:1260  Loss:1.151  Accuracy:0.80,0.40,0.40,0.68  Learning_rate:0.0010\n",
      "timeCost:3:01:34.360427\n",
      "Iter:1280  Loss:1.101  Accuracy:0.80,0.72,0.48,0.52  Learning_rate:0.0010\n",
      "timeCost:3:04:57.381266\n",
      "Iter:1300  Loss:0.905  Accuracy:0.88,0.72,0.52,0.76  Learning_rate:0.0010\n",
      "timeCost:3:08:20.596622\n",
      "Iter:1320  Loss:0.982  Accuracy:0.84,0.52,0.48,0.68  Learning_rate:0.0010\n",
      "timeCost:3:11:43.266527\n",
      "Iter:1340  Loss:0.867  Accuracy:0.76,0.64,0.64,0.64  Learning_rate:0.0010\n",
      "timeCost:3:15:06.235140\n",
      "Iter:1360  Loss:1.011  Accuracy:0.84,0.52,0.64,0.52  Learning_rate:0.0010\n",
      "timeCost:3:18:28.881985\n",
      "Iter:1380  Loss:0.993  Accuracy:0.68,0.48,0.60,0.76  Learning_rate:0.0010\n",
      "timeCost:3:21:51.886780\n",
      "Iter:1400  Loss:0.840  Accuracy:0.72,0.72,0.56,0.68  Learning_rate:0.0010\n",
      "timeCost:3:25:14.614840\n",
      "Iter:1420  Loss:1.039  Accuracy:0.76,0.44,0.60,0.56  Learning_rate:0.0010\n",
      "timeCost:3:28:15.414442\n",
      "Iter:1440  Loss:0.783  Accuracy:0.76,0.64,0.64,0.84  Learning_rate:0.0010\n",
      "timeCost:3:30:19.184550\n",
      "Iter:1460  Loss:0.902  Accuracy:0.72,0.72,0.64,0.72  Learning_rate:0.0010\n",
      "timeCost:3:32:39.377584\n",
      "Iter:1480  Loss:0.744  Accuracy:0.84,0.68,0.56,0.68  Learning_rate:0.0010\n",
      "timeCost:3:34:43.317143\n",
      "Iter:1500  Loss:0.846  Accuracy:0.84,0.60,0.60,0.72  Learning_rate:0.0010\n",
      "timeCost:3:36:47.360980\n",
      "Iter:1520  Loss:0.874  Accuracy:0.88,0.68,0.68,0.68  Learning_rate:0.0010\n",
      "timeCost:3:38:51.381754\n",
      "Iter:1540  Loss:0.785  Accuracy:0.76,0.60,0.72,0.80  Learning_rate:0.0010\n",
      "timeCost:3:40:55.415563\n",
      "Iter:1560  Loss:0.800  Accuracy:0.80,0.56,0.60,0.84  Learning_rate:0.0010\n",
      "timeCost:3:42:59.060338\n",
      "Iter:1580  Loss:0.687  Accuracy:0.76,0.72,0.64,0.88  Learning_rate:0.0010\n",
      "timeCost:3:45:02.749035\n",
      "Iter:1600  Loss:0.715  Accuracy:0.92,0.60,0.68,0.80  Learning_rate:0.0010\n",
      "timeCost:3:47:07.144807\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter:1620  Loss:0.612  Accuracy:0.80,0.72,0.68,0.80  Learning_rate:0.0010\n",
      "timeCost:3:49:11.433293\n",
      "Iter:1640  Loss:0.681  Accuracy:0.84,0.84,0.60,0.76  Learning_rate:0.0010\n",
      "timeCost:3:51:15.363828\n",
      "Iter:1660  Loss:0.456  Accuracy:0.92,0.76,0.80,0.84  Learning_rate:0.0010\n",
      "timeCost:3:53:19.231196\n",
      "Iter:1680  Loss:0.643  Accuracy:0.84,0.84,0.64,0.72  Learning_rate:0.0010\n",
      "timeCost:3:55:22.931117\n",
      "Iter:1700  Loss:0.450  Accuracy:0.92,0.72,0.84,0.88  Learning_rate:0.0010\n",
      "timeCost:3:57:26.718270\n",
      "Iter:1720  Loss:0.788  Accuracy:0.84,0.60,0.68,0.68  Learning_rate:0.0010\n",
      "timeCost:3:59:31.406319\n",
      "Iter:1740  Loss:0.489  Accuracy:0.84,0.80,0.80,0.76  Learning_rate:0.0010\n",
      "timeCost:4:01:37.078945\n",
      "Iter:1760  Loss:0.385  Accuracy:1.00,0.84,0.76,0.84  Learning_rate:0.0010\n",
      "timeCost:4:03:41.893831\n",
      "Iter:1780  Loss:0.429  Accuracy:0.92,0.96,0.76,0.76  Learning_rate:0.0010\n",
      "timeCost:4:05:49.745064\n",
      "Iter:1800  Loss:0.408  Accuracy:0.88,0.84,0.76,0.72  Learning_rate:0.0010\n",
      "timeCost:4:08:09.983496\n",
      "Iter:1820  Loss:0.622  Accuracy:0.96,0.76,0.76,0.68  Learning_rate:0.0010\n",
      "timeCost:4:10:31.011529\n",
      "Iter:1840  Loss:0.581  Accuracy:0.92,0.80,0.84,0.80  Learning_rate:0.0010\n",
      "timeCost:4:12:48.829555\n",
      "Iter:1860  Loss:0.525  Accuracy:0.92,0.76,0.80,0.76  Learning_rate:0.0010\n",
      "timeCost:4:15:05.396771\n",
      "Iter:1880  Loss:0.486  Accuracy:1.00,0.84,0.72,0.88  Learning_rate:0.0010\n",
      "timeCost:4:17:17.247292\n",
      "Iter:1900  Loss:0.509  Accuracy:0.88,0.72,0.80,0.84  Learning_rate:0.0010\n",
      "timeCost:4:19:21.615857\n",
      "Iter:1920  Loss:0.406  Accuracy:0.92,0.96,0.84,0.84  Learning_rate:0.0010\n",
      "timeCost:4:21:26.306890\n",
      "Iter:1940  Loss:0.412  Accuracy:0.92,0.88,0.88,0.84  Learning_rate:0.0010\n",
      "timeCost:4:23:35.797866\n",
      "Iter:1960  Loss:0.372  Accuracy:0.96,0.92,0.88,0.84  Learning_rate:0.0010\n",
      "timeCost:4:25:46.052641\n",
      "Iter:1980  Loss:0.372  Accuracy:0.88,0.92,0.84,0.92  Learning_rate:0.0010\n",
      "timeCost:4:27:56.090529\n",
      "Iter:2000  Loss:0.437  Accuracy:0.88,0.76,0.84,0.92  Learning_rate:0.0003\n",
      "timeCost:4:30:06.415574\n",
      "Iter:2020  Loss:0.344  Accuracy:0.84,0.84,0.84,0.88  Learning_rate:0.0003\n",
      "timeCost:4:32:15.898895\n",
      "Iter:2040  Loss:0.419  Accuracy:0.84,0.84,0.84,0.88  Learning_rate:0.0003\n",
      "timeCost:4:34:21.784602\n",
      "Iter:2060  Loss:0.409  Accuracy:0.96,0.84,0.76,0.88  Learning_rate:0.0003\n",
      "timeCost:4:36:27.549511\n",
      "Iter:2080  Loss:0.420  Accuracy:1.00,0.84,0.80,1.00  Learning_rate:0.0003\n",
      "timeCost:4:38:34.517185\n",
      "Iter:2100  Loss:0.221  Accuracy:0.96,0.84,0.88,0.96  Learning_rate:0.0003\n",
      "timeCost:4:40:40.263958\n",
      "Iter:2120  Loss:0.336  Accuracy:0.96,0.76,0.92,0.88  Learning_rate:0.0003\n",
      "timeCost:4:42:45.245174\n",
      "Iter:2140  Loss:0.258  Accuracy:0.96,0.84,0.84,0.96  Learning_rate:0.0003\n",
      "timeCost:4:44:50.290615\n",
      "Iter:2160  Loss:0.481  Accuracy:0.84,0.88,0.76,0.88  Learning_rate:0.0003\n",
      "timeCost:4:46:58.193350\n",
      "Iter:2180  Loss:0.422  Accuracy:0.92,0.88,0.88,0.76  Learning_rate:0.0003\n",
      "timeCost:4:49:06.816675\n",
      "Iter:2200  Loss:0.181  Accuracy:0.96,0.96,0.88,0.96  Learning_rate:0.0003\n",
      "timeCost:4:51:14.716143\n",
      "Iter:2220  Loss:0.290  Accuracy:0.88,0.76,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:4:53:27.631342\n",
      "Iter:2240  Loss:0.367  Accuracy:0.88,0.84,0.88,0.88  Learning_rate:0.0003\n",
      "timeCost:4:55:50.041597\n",
      "Iter:2260  Loss:0.351  Accuracy:0.96,0.80,0.92,0.84  Learning_rate:0.0003\n",
      "timeCost:4:57:58.095763\n",
      "Iter:2280  Loss:0.383  Accuracy:0.96,0.92,0.92,0.80  Learning_rate:0.0003\n",
      "timeCost:5:00:06.316677\n",
      "Iter:2300  Loss:0.347  Accuracy:0.96,0.92,0.96,0.72  Learning_rate:0.0003\n",
      "timeCost:5:02:15.824579\n",
      "Iter:2320  Loss:0.320  Accuracy:1.00,0.84,0.88,0.84  Learning_rate:0.0003\n",
      "timeCost:5:04:25.808255\n",
      "Iter:2340  Loss:0.263  Accuracy:0.92,0.88,0.92,0.92  Learning_rate:0.0003\n",
      "timeCost:5:06:35.041583\n",
      "Iter:2360  Loss:0.125  Accuracy:0.96,1.00,1.00,0.92  Learning_rate:0.0003\n",
      "timeCost:5:08:41.668234\n",
      "Iter:2380  Loss:0.304  Accuracy:0.84,0.88,0.88,0.84  Learning_rate:0.0003\n",
      "timeCost:5:10:48.848317\n",
      "Iter:2400  Loss:0.168  Accuracy:0.96,0.84,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:5:12:53.688550\n",
      "Iter:2420  Loss:0.400  Accuracy:0.88,0.80,0.92,0.88  Learning_rate:0.0003\n",
      "timeCost:5:14:58.545334\n",
      "Iter:2440  Loss:0.339  Accuracy:0.96,0.92,0.88,0.84  Learning_rate:0.0003\n",
      "timeCost:5:17:02.062122\n",
      "Iter:2460  Loss:0.224  Accuracy:1.00,0.92,0.88,0.88  Learning_rate:0.0003\n",
      "timeCost:5:19:05.792394\n",
      "Iter:2480  Loss:0.275  Accuracy:0.92,0.88,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:5:21:10.062216\n",
      "Iter:2500  Loss:0.198  Accuracy:1.00,1.00,0.84,0.88  Learning_rate:0.0003\n",
      "timeCost:5:23:15.235178\n",
      "Iter:2520  Loss:0.250  Accuracy:1.00,0.96,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:5:25:20.867699\n",
      "Iter:2540  Loss:0.175  Accuracy:0.96,0.92,0.92,0.88  Learning_rate:0.0003\n",
      "timeCost:5:27:26.787635\n",
      "Iter:2560  Loss:0.191  Accuracy:0.96,0.88,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:5:29:34.227646\n",
      "Iter:2580  Loss:0.317  Accuracy:0.96,0.88,0.88,0.88  Learning_rate:0.0003\n",
      "timeCost:5:31:39.926755\n",
      "Iter:2600  Loss:0.230  Accuracy:0.96,0.80,0.88,0.96  Learning_rate:0.0003\n",
      "timeCost:5:33:46.012893\n",
      "Iter:2620  Loss:0.198  Accuracy:1.00,0.96,0.92,0.92  Learning_rate:0.0003\n",
      "timeCost:5:35:50.813859\n",
      "Iter:2640  Loss:0.218  Accuracy:1.00,0.92,0.92,0.92  Learning_rate:0.0003\n",
      "timeCost:5:37:55.333351\n",
      "Iter:2660  Loss:0.180  Accuracy:0.96,0.92,0.92,0.92  Learning_rate:0.0003\n",
      "timeCost:5:39:59.617830\n",
      "Iter:2680  Loss:0.253  Accuracy:1.00,0.88,0.96,0.88  Learning_rate:0.0003\n",
      "timeCost:5:42:04.223394\n",
      "Iter:2700  Loss:0.182  Accuracy:1.00,0.96,0.84,0.96  Learning_rate:0.0003\n",
      "timeCost:5:44:10.516540\n",
      "Iter:2720  Loss:0.129  Accuracy:1.00,0.96,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:5:46:16.145461\n",
      "Iter:2740  Loss:0.208  Accuracy:0.92,0.92,0.96,0.88  Learning_rate:0.0003\n",
      "timeCost:5:48:23.042363\n",
      "Iter:2760  Loss:0.169  Accuracy:1.00,0.92,0.88,0.88  Learning_rate:0.0003\n",
      "timeCost:5:51:33.675177\n",
      "Iter:2780  Loss:0.286  Accuracy:0.96,0.84,0.84,0.92  Learning_rate:0.0003\n",
      "timeCost:5:55:04.703307\n",
      "Iter:2800  Loss:0.273  Accuracy:0.92,0.92,0.80,1.00  Learning_rate:0.0003\n",
      "timeCost:5:58:42.562752\n",
      "Iter:2820  Loss:0.245  Accuracy:0.96,0.88,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:6:02:13.987784\n",
      "Iter:2840  Loss:0.416  Accuracy:0.92,0.84,0.84,0.80  Learning_rate:0.0003\n",
      "timeCost:6:05:41.281999\n",
      "Iter:2860  Loss:0.214  Accuracy:0.96,0.96,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:6:09:07.759962\n",
      "Iter:2880  Loss:0.146  Accuracy:0.96,0.96,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:6:12:33.634000\n",
      "Iter:2900  Loss:0.345  Accuracy:0.92,0.88,0.84,0.88  Learning_rate:0.0003\n",
      "timeCost:6:15:59.371117\n",
      "Iter:2920  Loss:0.199  Accuracy:0.92,0.88,0.92,0.92  Learning_rate:0.0003\n",
      "timeCost:6:19:24.982827\n",
      "Iter:2940  Loss:0.156  Accuracy:0.96,0.96,0.92,0.88  Learning_rate:0.0003\n",
      "timeCost:6:22:51.024817\n",
      "Iter:2960  Loss:0.189  Accuracy:0.88,0.92,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:6:26:16.787110\n",
      "Iter:2980  Loss:0.068  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0003\n",
      "timeCost:6:29:42.594880\n",
      "Iter:3000  Loss:0.175  Accuracy:1.00,1.00,0.96,0.88  Learning_rate:0.0003\n",
      "timeCost:6:33:09.423500\n",
      "Iter:3020  Loss:0.314  Accuracy:0.92,0.92,0.88,0.80  Learning_rate:0.0003\n",
      "timeCost:6:36:35.319121\n",
      "Iter:3040  Loss:0.116  Accuracy:1.00,0.96,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:6:40:01.561715\n",
      "Iter:3060  Loss:0.207  Accuracy:1.00,0.88,0.84,0.92  Learning_rate:0.0003\n",
      "timeCost:6:43:27.150738\n",
      "Iter:3080  Loss:0.357  Accuracy:0.96,0.76,0.88,0.84  Learning_rate:0.0003\n",
      "timeCost:6:46:53.620305\n",
      "Iter:3100  Loss:0.119  Accuracy:0.96,0.96,1.00,0.92  Learning_rate:0.0003\n",
      "timeCost:6:50:19.475273\n",
      "Iter:3120  Loss:0.261  Accuracy:0.96,0.84,0.88,1.00  Learning_rate:0.0003\n",
      "timeCost:6:53:44.289516\n",
      "Iter:3140  Loss:0.188  Accuracy:0.96,0.92,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:6:57:10.257400\n",
      "Iter:3160  Loss:0.152  Accuracy:1.00,0.92,0.88,1.00  Learning_rate:0.0003\n",
      "timeCost:7:00:36.253295\n",
      "Iter:3180  Loss:0.133  Accuracy:1.00,0.96,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:7:04:03.200796\n",
      "Iter:3200  Loss:0.163  Accuracy:1.00,0.88,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:7:07:29.598649\n",
      "Iter:3220  Loss:0.100  Accuracy:0.96,1.00,0.96,0.88  Learning_rate:0.0003\n",
      "timeCost:7:10:58.484956\n",
      "Iter:3240  Loss:0.067  Accuracy:1.00,0.96,0.96,1.00  Learning_rate:0.0003\n",
      "timeCost:7:14:27.967360\n",
      "Iter:3260  Loss:0.156  Accuracy:1.00,0.92,0.88,1.00  Learning_rate:0.0003\n",
      "timeCost:7:17:56.640226\n",
      "Iter:3280  Loss:0.119  Accuracy:0.92,0.96,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:7:21:24.669279\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter:3300  Loss:0.112  Accuracy:0.92,0.96,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:7:24:53.527365\n",
      "Iter:3320  Loss:0.155  Accuracy:1.00,1.00,0.92,0.92  Learning_rate:0.0003\n",
      "timeCost:7:28:22.274430\n",
      "Iter:3340  Loss:0.166  Accuracy:1.00,0.92,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:7:31:51.199968\n",
      "Iter:3360  Loss:0.174  Accuracy:0.96,0.96,0.84,1.00  Learning_rate:0.0003\n",
      "timeCost:7:35:20.145562\n",
      "Iter:3380  Loss:0.146  Accuracy:1.00,0.92,1.00,0.92  Learning_rate:0.0003\n",
      "timeCost:7:38:48.642926\n",
      "Iter:3400  Loss:0.084  Accuracy:1.00,0.96,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:7:42:18.000614\n",
      "Iter:3420  Loss:0.257  Accuracy:0.96,0.92,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:7:45:46.234313\n",
      "Iter:3440  Loss:0.099  Accuracy:1.00,0.92,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:7:49:14.965169\n",
      "Iter:3460  Loss:0.208  Accuracy:0.92,0.88,0.88,0.96  Learning_rate:0.0003\n",
      "timeCost:7:51:55.374998\n",
      "Iter:3480  Loss:0.205  Accuracy:1.00,0.92,0.88,0.96  Learning_rate:0.0003\n",
      "timeCost:7:54:00.240769\n",
      "Iter:3500  Loss:0.179  Accuracy:0.88,0.88,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:7:56:05.314181\n",
      "Iter:3520  Loss:0.095  Accuracy:1.00,0.96,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:7:58:10.088078\n",
      "Iter:3540  Loss:0.113  Accuracy:0.92,0.96,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:8:00:14.977462\n",
      "Iter:3560  Loss:0.207  Accuracy:1.00,1.00,0.84,0.88  Learning_rate:0.0003\n",
      "timeCost:8:02:20.494298\n",
      "Iter:3580  Loss:0.028  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0003\n",
      "timeCost:8:04:26.003271\n",
      "Iter:3600  Loss:0.201  Accuracy:1.00,0.88,0.88,0.88  Learning_rate:0.0003\n",
      "timeCost:8:06:30.942689\n",
      "Iter:3620  Loss:0.114  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:8:08:36.748990\n",
      "Iter:3640  Loss:0.195  Accuracy:0.96,0.92,0.96,0.80  Learning_rate:0.0003\n",
      "timeCost:8:10:42.399303\n",
      "Iter:3660  Loss:0.208  Accuracy:1.00,0.84,0.84,1.00  Learning_rate:0.0003\n",
      "timeCost:8:12:47.489494\n",
      "Iter:3680  Loss:0.065  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:8:14:54.283030\n",
      "Iter:3700  Loss:0.131  Accuracy:0.96,1.00,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:8:17:00.743563\n",
      "Iter:3720  Loss:0.132  Accuracy:0.96,0.92,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:8:19:08.763820\n",
      "Iter:3740  Loss:0.174  Accuracy:1.00,0.88,0.88,0.92  Learning_rate:0.0003\n",
      "timeCost:8:21:15.196680\n",
      "Iter:3760  Loss:0.064  Accuracy:1.00,0.96,0.96,1.00  Learning_rate:0.0003\n",
      "timeCost:8:23:19.660094\n",
      "Iter:3780  Loss:0.219  Accuracy:0.96,0.96,0.92,0.84  Learning_rate:0.0003\n",
      "timeCost:8:25:23.692162\n",
      "Iter:3800  Loss:0.176  Accuracy:0.92,0.96,0.96,1.00  Learning_rate:0.0003\n",
      "timeCost:8:27:27.681288\n",
      "Iter:3820  Loss:0.165  Accuracy:0.96,0.96,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:8:29:31.524247\n",
      "Iter:3840  Loss:0.136  Accuracy:1.00,0.92,0.92,0.96  Learning_rate:0.0003\n",
      "timeCost:8:31:35.559154\n",
      "Iter:3860  Loss:0.167  Accuracy:0.96,0.92,0.88,0.96  Learning_rate:0.0003\n",
      "timeCost:8:33:39.543228\n",
      "Iter:3880  Loss:0.175  Accuracy:0.96,0.92,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:8:35:43.368870\n",
      "Iter:3900  Loss:0.208  Accuracy:0.88,1.00,1.00,0.92  Learning_rate:0.0003\n",
      "timeCost:8:37:47.519786\n",
      "Iter:3920  Loss:0.097  Accuracy:0.92,1.00,1.00,0.96  Learning_rate:0.0003\n",
      "timeCost:8:39:51.835285\n",
      "Iter:3940  Loss:0.090  Accuracy:1.00,0.96,0.92,1.00  Learning_rate:0.0003\n",
      "timeCost:8:41:55.802901\n",
      "Iter:3960  Loss:0.098  Accuracy:1.00,0.92,0.96,1.00  Learning_rate:0.0003\n",
      "timeCost:8:44:00.061336\n",
      "Iter:3980  Loss:0.058  Accuracy:1.00,0.96,0.96,0.96  Learning_rate:0.0003\n",
      "timeCost:8:46:04.304689\n",
      "Iter:4000  Loss:0.068  Accuracy:1.00,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:8:48:08.484588\n",
      "Iter:4020  Loss:0.111  Accuracy:0.96,0.96,0.92,1.00  Learning_rate:0.0001\n",
      "timeCost:8:50:13.055777\n",
      "Iter:4040  Loss:0.106  Accuracy:0.96,1.00,0.88,0.92  Learning_rate:0.0001\n",
      "timeCost:8:52:17.623284\n",
      "Iter:4060  Loss:0.096  Accuracy:1.00,0.92,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:8:54:21.926574\n",
      "Iter:4080  Loss:0.049  Accuracy:1.00,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:8:56:26.310187\n",
      "Iter:4100  Loss:0.115  Accuracy:0.96,0.92,0.92,1.00  Learning_rate:0.0001\n",
      "timeCost:8:58:30.655262\n",
      "Iter:4120  Loss:0.118  Accuracy:1.00,0.88,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:9:00:34.906482\n",
      "Iter:4140  Loss:0.070  Accuracy:1.00,0.92,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:9:02:39.386817\n",
      "Iter:4160  Loss:0.113  Accuracy:0.96,0.96,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:9:04:43.475712\n",
      "Iter:4180  Loss:0.158  Accuracy:0.92,0.96,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:9:06:47.771392\n",
      "Iter:4200  Loss:0.016  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:9:08:52.022669\n",
      "Iter:4220  Loss:0.128  Accuracy:0.96,0.96,0.96,0.92  Learning_rate:0.0001\n",
      "timeCost:9:10:56.841993\n",
      "Iter:4240  Loss:0.098  Accuracy:0.96,1.00,0.96,0.88  Learning_rate:0.0001\n",
      "timeCost:9:13:01.212716\n",
      "Iter:4260  Loss:0.106  Accuracy:0.92,1.00,1.00,0.92  Learning_rate:0.0001\n",
      "timeCost:9:15:05.985406\n",
      "Iter:4280  Loss:0.046  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:9:17:11.296949\n",
      "Iter:4300  Loss:0.079  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:9:19:16.348734\n",
      "Iter:4320  Loss:0.034  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:9:21:21.684984\n",
      "Iter:4340  Loss:0.111  Accuracy:0.96,1.00,0.96,0.92  Learning_rate:0.0001\n",
      "timeCost:9:23:27.289757\n",
      "Iter:4360  Loss:0.198  Accuracy:0.92,0.88,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:9:25:32.566017\n",
      "Iter:4380  Loss:0.093  Accuracy:1.00,0.96,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:9:27:38.646910\n",
      "Iter:4400  Loss:0.067  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:9:29:44.090176\n",
      "Iter:4420  Loss:0.095  Accuracy:1.00,0.96,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:9:31:49.619042\n",
      "Iter:4440  Loss:0.076  Accuracy:1.00,0.96,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:9:33:54.878051\n",
      "Iter:4460  Loss:0.051  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:9:36:00.420963\n",
      "Iter:4480  Loss:0.086  Accuracy:0.96,0.96,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:9:38:05.898590\n",
      "Iter:4500  Loss:0.083  Accuracy:0.96,0.96,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:9:40:11.309913\n",
      "Iter:4520  Loss:0.062  Accuracy:1.00,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:9:42:16.468778\n",
      "Iter:4540  Loss:0.046  Accuracy:1.00,1.00,1.00,0.92  Learning_rate:0.0001\n",
      "timeCost:9:44:21.513884\n",
      "Iter:4560  Loss:0.049  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:9:46:26.614653\n",
      "Iter:4580  Loss:0.103  Accuracy:0.96,0.92,1.00,0.92  Learning_rate:0.0001\n",
      "timeCost:9:48:31.790199\n",
      "Iter:4600  Loss:0.054  Accuracy:1.00,0.96,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:9:50:37.302062\n",
      "Iter:4620  Loss:0.079  Accuracy:0.96,1.00,0.92,1.00  Learning_rate:0.0001\n",
      "timeCost:9:52:42.796939\n",
      "Iter:4640  Loss:0.032  Accuracy:1.00,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:9:54:48.167126\n",
      "Iter:4660  Loss:0.027  Accuracy:0.96,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:9:56:53.605573\n",
      "Iter:4680  Loss:0.076  Accuracy:1.00,0.88,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:9:58:58.936031\n",
      "Iter:4700  Loss:0.079  Accuracy:0.92,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:10:01:03.965992\n",
      "Iter:4720  Loss:0.088  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:10:03:09.033882\n",
      "Iter:4740  Loss:0.028  Accuracy:1.00,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:05:14.009672\n",
      "Iter:4760  Loss:0.106  Accuracy:1.00,0.92,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:07:18.858250\n",
      "Iter:4780  Loss:0.052  Accuracy:0.96,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:09:24.227533\n",
      "Iter:4800  Loss:0.059  Accuracy:0.92,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:11:30.034314\n",
      "Iter:4820  Loss:0.024  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:13:35.292605\n",
      "Iter:4840  Loss:0.032  Accuracy:1.00,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:15:40.178317\n",
      "Iter:4860  Loss:0.116  Accuracy:0.92,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:17:45.914139\n",
      "Iter:4880  Loss:0.057  Accuracy:1.00,0.96,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:19:50.974156\n",
      "Iter:4900  Loss:0.046  Accuracy:1.00,0.96,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:21:55.871828\n",
      "Iter:4920  Loss:0.099  Accuracy:0.96,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:10:24:00.852667\n",
      "Iter:4940  Loss:0.044  Accuracy:0.96,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:26:06.330803\n",
      "Iter:4960  Loss:0.089  Accuracy:1.00,0.92,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:10:28:11.282837\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter:4980  Loss:0.086  Accuracy:1.00,0.92,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:30:16.608472\n",
      "Iter:5000  Loss:0.065  Accuracy:0.96,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:32:21.953454\n",
      "Iter:5020  Loss:0.040  Accuracy:1.00,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:34:26.739370\n",
      "Iter:5040  Loss:0.028  Accuracy:0.96,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:36:31.642943\n",
      "Iter:5060  Loss:0.083  Accuracy:0.96,0.96,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:38:36.815514\n",
      "Iter:5080  Loss:0.075  Accuracy:1.00,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:40:41.648086\n",
      "Iter:5100  Loss:0.070  Accuracy:1.00,0.92,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:42:46.564897\n",
      "Iter:5120  Loss:0.023  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:44:51.693130\n",
      "Iter:5140  Loss:0.037  Accuracy:0.96,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:46:56.980978\n",
      "Iter:5160  Loss:0.105  Accuracy:1.00,0.92,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:10:49:02.028762\n",
      "Iter:5180  Loss:0.139  Accuracy:0.96,1.00,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:10:51:07.281676\n",
      "Iter:5200  Loss:0.066  Accuracy:0.96,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:53:12.500796\n",
      "Iter:5220  Loss:0.030  Accuracy:0.96,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:10:55:17.541078\n",
      "Iter:5240  Loss:0.074  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:10:57:22.920422\n",
      "Iter:5260  Loss:0.081  Accuracy:0.96,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:10:59:28.119502\n",
      "Iter:5280  Loss:0.111  Accuracy:1.00,0.92,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:01:33.237782\n",
      "Iter:5300  Loss:0.109  Accuracy:1.00,0.92,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:11:03:38.461755\n",
      "Iter:5320  Loss:0.024  Accuracy:1.00,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:05:43.652063\n",
      "Iter:5340  Loss:0.182  Accuracy:0.96,0.92,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:11:07:49.372162\n",
      "Iter:5360  Loss:0.066  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:11:09:54.654040\n",
      "Iter:5380  Loss:0.040  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:11:12:00.000168\n",
      "Iter:5400  Loss:0.096  Accuracy:1.00,0.96,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:11:14:05.149624\n",
      "Iter:5420  Loss:0.132  Accuracy:1.00,0.92,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:11:16:10.400494\n",
      "Iter:5440  Loss:0.063  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:11:18:15.472827\n",
      "Iter:5460  Loss:0.043  Accuracy:0.96,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:20:20.715921\n",
      "Iter:5480  Loss:0.126  Accuracy:0.96,0.96,1.00,0.92  Learning_rate:0.0001\n",
      "timeCost:11:22:26.186826\n",
      "Iter:5500  Loss:0.079  Accuracy:1.00,1.00,0.92,1.00  Learning_rate:0.0001\n",
      "timeCost:11:24:31.514395\n",
      "Iter:5520  Loss:0.052  Accuracy:0.96,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:11:26:36.175215\n",
      "Iter:5540  Loss:0.048  Accuracy:0.96,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:11:28:40.541172\n",
      "Iter:5560  Loss:0.135  Accuracy:0.96,1.00,0.96,0.92  Learning_rate:0.0001\n",
      "timeCost:11:30:45.066253\n",
      "Iter:5580  Loss:0.019  Accuracy:1.00,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:32:49.568736\n",
      "Iter:5600  Loss:0.093  Accuracy:1.00,0.96,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:11:34:53.955467\n",
      "Iter:5620  Loss:0.017  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:36:58.622105\n",
      "Iter:5640  Loss:0.013  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:39:04.826400\n",
      "Iter:5660  Loss:0.044  Accuracy:0.96,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:41:09.309311\n",
      "Iter:5680  Loss:0.111  Accuracy:0.96,0.96,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:11:43:13.787876\n",
      "Iter:5700  Loss:0.064  Accuracy:1.00,0.92,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:11:45:18.364860\n",
      "Iter:5720  Loss:0.019  Accuracy:1.00,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:11:47:22.740498\n",
      "Iter:5740  Loss:0.093  Accuracy:0.92,0.96,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:11:49:26.858361\n",
      "Iter:5760  Loss:0.091  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:11:51:31.153085\n",
      "Iter:5780  Loss:0.101  Accuracy:0.92,1.00,1.00,0.92  Learning_rate:0.0001\n",
      "timeCost:11:53:35.530258\n",
      "Iter:5800  Loss:0.124  Accuracy:1.00,1.00,1.00,0.92  Learning_rate:0.0001\n",
      "timeCost:11:55:40.038793\n",
      "Iter:5820  Loss:0.083  Accuracy:1.00,0.96,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:11:57:53.312149\n",
      "Iter:5840  Loss:0.062  Accuracy:1.00,0.96,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:12:00:17.592717\n",
      "Iter:5860  Loss:0.098  Accuracy:0.96,0.96,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:12:02:39.148193\n",
      "Iter:5880  Loss:0.100  Accuracy:0.96,1.00,0.92,0.96  Learning_rate:0.0001\n",
      "timeCost:12:05:01.085422\n",
      "Iter:5900  Loss:0.055  Accuracy:0.92,1.00,1.00,1.00  Learning_rate:0.0001\n",
      "timeCost:12:07:21.063441\n",
      "Iter:5920  Loss:0.065  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:12:09:39.751804\n",
      "Iter:5940  Loss:0.048  Accuracy:1.00,1.00,0.96,0.96  Learning_rate:0.0001\n",
      "timeCost:12:11:56.127876\n",
      "Iter:5960  Loss:0.026  Accuracy:1.00,1.00,0.96,1.00  Learning_rate:0.0001\n",
      "timeCost:12:14:05.549073\n",
      "Iter:5980  Loss:0.057  Accuracy:1.00,1.00,1.00,0.96  Learning_rate:0.0001\n",
      "timeCost:12:16:20.589128\n",
      "Iter:6000  Loss:0.149  Accuracy:0.92,0.92,0.92,0.92  Learning_rate:0.0000\n",
      "timeCost:12:18:39.941023\n",
      "Completed!\n"
     ]
    }
   ],
   "source": [
    "# 获取图片数据和标签\n",
    "image, label0, label1, label2, label3 = read_and_decode(TFRECORD_FILE)\n",
    "\n",
    "#使用shuffle_batch可以随机打乱\n",
    "image_batch, label_batch0, label_batch1, label_batch2, label_batch3 = tf.train.shuffle_batch(\n",
    "        [image, label0, label1, label2, label3], batch_size = BATCH_SIZE,\n",
    "        capacity = 50000, min_after_dequeue=10000, num_threads=1)\n",
    "\n",
    "#定义网络结构\n",
    "train_network_fn = nets_factory.get_network_fn(\n",
    "    'alexnet_v2',\n",
    "    num_classes=CHAR_SET_LEN,\n",
    "    weight_decay=0.0005,\n",
    "    is_training=True)\n",
    " \n",
    "    \n",
    "with tf.Session() as sess:\n",
    "    # inputs: a tensor of size [batch_size, height, width, channels]\n",
    "    X = tf.reshape(x, [BATCH_SIZE, 224, 224, 1])\n",
    "    # 数据输入网络得到输出值\n",
    "    logits0,logits1,logits2,logits3,end_points = train_network_fn(X)\n",
    "    \n",
    "    # 把标签转成one_hot的形式\n",
    "    one_hot_labels0 = tf.one_hot(indices=tf.cast(y0, tf.int32), depth=CHAR_SET_LEN)\n",
    "    one_hot_labels1 = tf.one_hot(indices=tf.cast(y1, tf.int32), depth=CHAR_SET_LEN)\n",
    "    one_hot_labels2 = tf.one_hot(indices=tf.cast(y2, tf.int32), depth=CHAR_SET_LEN)\n",
    "    one_hot_labels3 = tf.one_hot(indices=tf.cast(y3, tf.int32), depth=CHAR_SET_LEN)\n",
    "    \n",
    "    # 计算loss\n",
    "    loss0 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits0,labels=one_hot_labels0)) \n",
    "    loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits1,labels=one_hot_labels1)) \n",
    "    loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits2,labels=one_hot_labels2)) \n",
    "    loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits3,labels=one_hot_labels3)) \n",
    "    # 计算总的loss\n",
    "    total_loss = (loss0+loss1+loss2+loss3)/4.0\n",
    "    # 优化total_loss\n",
    "    optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(total_loss) \n",
    "    \n",
    "    # 计算准确率\n",
    "    correct_prediction0 = tf.equal(tf.argmax(one_hot_labels0,1),tf.argmax(logits0,1))\n",
    "    accuracy0 = tf.reduce_mean(tf.cast(correct_prediction0,tf.float32))\n",
    "    \n",
    "    correct_prediction1 = tf.equal(tf.argmax(one_hot_labels1,1),tf.argmax(logits1,1))\n",
    "    accuracy1 = tf.reduce_mean(tf.cast(correct_prediction1,tf.float32))\n",
    "    \n",
    "    correct_prediction2 = tf.equal(tf.argmax(one_hot_labels2,1),tf.argmax(logits2,1))\n",
    "    accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2,tf.float32))\n",
    "    \n",
    "    correct_prediction3 = tf.equal(tf.argmax(one_hot_labels3,1),tf.argmax(logits3,1))\n",
    "    accuracy3 = tf.reduce_mean(tf.cast(correct_prediction3,tf.float32)) \n",
    "    \n",
    "    # 用于保存模型\n",
    "    saver = tf.train.Saver()\n",
    "    # 初始化\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    \n",
    "    # 创建一个协调器，管理线程\n",
    "    coord = tf.train.Coordinator()\n",
    "    # 启动QueueRunner, 此时文件名队列已经进队\n",
    "    threads = tf.train.start_queue_runners(sess=sess, coord=coord)\n",
    "    \n",
    "    print('开始进行训练(CPU):')\n",
    "    startTime=datetime.datetime.now()\n",
    "    for i in range(6001):\n",
    "        # 获取一个批次的数据和标签\n",
    "        b_image, b_label0, b_label1 ,b_label2 ,b_label3 = sess.run([image_batch, label_batch0, label_batch1, label_batch2, label_batch3])\n",
    "        # 优化模型\n",
    "        sess.run(optimizer, feed_dict={x: b_image, y0:b_label0, y1: b_label1, y2: b_label2, y3: b_label3})  \n",
    "\n",
    "        # 每迭代20次计算一次loss和准确率  \n",
    "        if i % 20 == 0:  \n",
    "            # 每迭代2000次降低一次学习率\n",
    "            if i%2000 == 0:\n",
    "                sess.run(tf.assign(lr, lr/3))\n",
    "            acc0,acc1,acc2,acc3,loss_ = sess.run([accuracy0,accuracy1,accuracy2,accuracy3,total_loss],feed_dict={x: b_image,\n",
    "                                                                                                                y0: b_label0,\n",
    "                                                                                                                y1: b_label1,\n",
    "                                                                                                                y2: b_label2,\n",
    "                                                                                                                y3: b_label3})      \n",
    "            learning_rate = sess.run(lr)\n",
    "            tempTime=datetime.datetime.now()-startTime\n",
    "            print (\"Iter:%d  Loss:%.3f  Accuracy:%.2f,%.2f,%.2f,%.2f  Learning_rate:%.4f\" % (i,loss_,acc0,acc1,acc2,acc3,learning_rate))\n",
    "            print('timeCost:'+str(tempTime))\n",
    "            \n",
    "            # 保存模型\n",
    "            # if acc0 > 0.90 and acc1 > 0.90 and acc2 > 0.90 and acc3 > 0.90: \n",
    "            if i==6000:\n",
    "                saver.save(sess, \"./captcha/models/crack_captcha.model\", global_step=i)  \n",
    "                break \n",
    "                \n",
    "    # 通知其他线程关闭\n",
    "    coord.request_stop()\n",
    "    # 其他所有线程关闭之后，这一函数才能返回\n",
    "    coord.join(threads)\n",
    "    print('Completed!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
